package com.work.order.controller;

import com.work.order.service.OrderService;
import com.work.order.service.TccOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;


@RestController
@RequestMapping("order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private TccOrderService tccOrderService;


    /**
     * XA/AT
     * 下单：插入订单表、扣减库存，product-1 正常
     * /order/placeOrder/commit
     * @return
     */
    @RequestMapping("/placeOrder/commit")
    public Boolean placeOrderCommit() {

        orderService.placeOrder("1", "product-1", 1);
        return true;
    }

    /**
     * XA/AT
     * 下单：插入订单表、扣减库存，product-2 ，模拟回滚
     *
     * @return
     */
    @RequestMapping("/placeOrder/rollback")
    public Boolean placeOrderRollback() {
        // product-2 扣库存时模拟了一个业务异常,
        orderService.placeOrder("1", "product-2", 1);
        return true;
    }

    /**
     * AT 模式测试
     * 创建订单、扣除金额、扣除库存、修改订单状态
     * @param userId
     * @param commodityCode
     * @param count
     * @return
     */
    @RequestMapping("/create")
    public Boolean create(String userId, String commodityCode, Integer count) {
        orderService.create(userId, commodityCode, count);
//        orderService.create("1", "product-1", 1);
        return true;
    }


    /**
     * 测试 TCC 模式；TCC测试入口
     * @param userId
     * @param commodityCode
     * @param count
     * @return
     */
    @RequestMapping("/placeOrderTcc")
    public Boolean placeOrderTcc(String userId, String commodityCode, Integer count) {
        orderService.orderTccAction(userId, commodityCode, count);
        return true;
    }
}
